<template>
  <div class="adup-import-sfc-order">
    <el-dialog
      v-el-drag-dialog="{ drag: true, full: false, resize: false }"
      :visible="true"
      :close-on-click-modal="false"
      @close="closeMd"
      :title="`${obj ? '修改' : '添加'}导入订单`"
      width="400px"
      :append-to-body="true">
      <el-form :model="fm.ps" :rules="fm.rules" :ref="fm.fname"
        @keyup.enter.native="onSubmit" :label-width="'100px'" v-if="obj">

        <el-form-item label="团号">
          <el-input :value="obj.travelNum" readonly></el-input>
        </el-form-item>

        <el-form-item :label="`${obj.type == 1 ? '机场' : '火车站'}选择`" prop="stationId">
          <el-select 
            v-model="fm.ps.stationId" 
            placeholder="请选择站点" 
            style="width: 100%">
            <el-option
              v-for="it in stationArr"
              :key="it.id"
              :label="it.name"
              :value="it.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="`${obj.type == 1 ? '航班号' : '车次号'}`" prop="wayNum">
          <el-input 
            v-model="fm.ps.wayNum" 
            :placeholder="`请填写${obj.type == 1 ? '航班号' : '车次号'}`" 
            maxlength="15"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button v-if="obj"
          type="success" 
          icon="el-icon-refresh-right" 
          @click="resetForm">
          重 置
        </el-button>

        <el-button 
          type="primary" 
          :icon="obj ? 'el-icon-edit' : 'el-icon-plus'" 
          @click="onSubmit" 
          :loading="fm.sb.load">
          {{obj ? '修 改' : '添 加'}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-drag-dialog';

import * as commonSer from '@/api/services/common.service';
import * as orderSer from '@/api/services/order.service';

export default {
  name: 'AdupImportSfcOrder',
  directives: { elDragDialog },
  props: { myData: Object },
  data() {
    return {
      // 修改对象数据
      obj: this.myData.obj,

      fm: {// 表单数据对象
        fname: "adupImportSfcOrderFm",
        sb: { load: false },
        ps: {
          // 导入订单id
          orderId: '',
          // 站点id与班次号任意传一个
          stationId: '',
          // 班次号与站点id任意传一个
          wayNum: '',
        },
        rules: {
          
        }
      },

      // 站点数组
      stationArr: [],
      
    }
  },
  async created() {
    const that = this;

    // 获取-站点列表
    let staRes = await commonSer.getStationList({type: that.obj.type});
    if(staRes.code == 1) that.stationArr = staRes.data;
    console.log('站点列表', staRes);

    // 设置表单默认数据
    that.setInitData();
  },
  methods:{
    closeMd() {
      this.$emit("myClick", {state: false});
    },
    // 设置初始化数据
    setInitData(){
      const that = this;

      // 设置-修改回填
      if(that.obj) {
        let o = that.obj;
        console.log('修改对象', o);
        let ps = {
          // 修改导入订单id
          orderId: o.id,
          // 站点id与班次号任意传一个
          stationId: o.stationId ? o.stationId.id : null,
          // 班次号与站点id任意传一个
          wayNum: '',
        };
        that.fm.ps = ps;
      }
    },
    // 重置表单
    resetForm() {
      this.$refs[this.fm.fname].resetFields();
      this.setInitData();
    },
    // 提交表单
    onSubmit() {
      let that = this, U = that.U, fm = that.fm, sb = fm.sb, ps = U.scopy(fm.ps);
      console.log('提交参数', ps);
      that.$refs[fm.fname].validate((valid) => {
        if (!valid) { return false; } else {
          let fg = true, p = {};
          // 验证
          if(fg){
            if(!ps.orderId){
              fg = U.qerrorFalse('[修改订单id]为空，请重新选择订单');
            }else{
              p.orderId = ps.orderId;
            }
          }

          if(fg){
            if(!ps.stationId && !ps.wayNum){
              fg = U.qerrorFalse('请至少操作一项');
            }else if(ps.stationId && ps.wayNum){
              fg = U.qerrorFalse('两项任选其一');
            }else if(ps.stationId){
              p.stationId = ps.stationId;
            }else if(ps.wayNum){
              p.wayNum = ps.wayNum;
            }
          }

          if(fg){
            sb.load = true;
            orderSer.adupImportSfcOrder(ps).then((res) => {
              sb.load = false;

              if (res.code != 1) {
                U.qerror(res.msg);
              } else {
                U.qsuccess(res.msg);

                // 关闭弹框
                that.closeMd();

                // 调用父组件方法-重新加载数据
                that.$emit("reloadDg");
              }
            });

            setTimeout(() => { sb.load = false; }, 10000);
          }
        }
      });
    },
    
  },
 }
</script>